//
//  LoginController.m
//  Safflower
//
//  Created by 袁亮 on 2016/11/28.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "LoginController.h"
#import "LoginView.h"
#import "UserDataCenter.h"
#import "HomeController.h"
#import "RegistVerifyController.h"

@interface LoginController ()<LoginViewDelegate>

@property (nonatomic, strong) LoginView *loginView;
@property (nonatomic, strong) UserDataCenter *dataCenter;
@property (nonatomic, strong) HomeController *homeController;

@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    _dataCenter = [[UserDataCenter alloc]init];
    

    
    [self makeLoginView];
}

- (void) makeLoginView
{
    _loginView = [[LoginView alloc]init];
    _loginView.delegate = self;
    [self.view addSubview:_loginView];
    [_loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
}

- (void)clickLoginBtnWithPhone:(NSString *)phone password:(NSString *)password
{
    if ([phone isEqualToString:@""] || phone == nil) {
        [YJProgressHud showErrorHud:@"请输入手机号"];
    }else if ([NSString checkIsPhone:phone] == NO || phone.length > 11){
        [YJProgressHud showErrorHud:@"请输入正确的手机号码"];
    }else {
        if ([password isEqualToString:@""] || password == nil) {
            [YJProgressHud showErrorHud:@"请输入密码"];
        }else{
            [YJProgressHud showLoadingHud:@"正在登陆"];
            [_dataCenter userLoginWithPhone:phone password:password complation:^(id data) {

                if (data == nil) {
                    [YJProgressHud dissmiss];
                    AppDelegate *appDelegate = [AppDelegate shareDelegate];
                    _homeController = [[HomeController alloc]init];
                    appDelegate.window.rootViewController = _homeController;
                }else{
                    [YJProgressHud showErrorHud:(NSString *)data];
                }
            }];
        }
    }
}

- (void)clickRegistBtn:(UIButton *)sender
{
    RegistVerifyController *verifyController = [[RegistVerifyController alloc]init];
    BaseNavigationController *verifyNavigation = [[BaseNavigationController alloc]initWithRootViewController:verifyController];
    [self presentViewController:verifyNavigation animated:NO completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
